const {Op} = require('sequelize')
const roleModel = require('../models/roleModel')

module.exports = {
    getAllRoles: async() => {
        let whereObj = {}
        try{
            let roles = await roleModel.findAll({
                where:whereObj,
                order: [['id','ASC']]// 升序排序
            })
            return roles
        } catch(error) {
            console.log("find all role error:",error)
            return false
        }
    },
    createRole: async(roleName,roleLevel) => {
        try {
            let result = await roleModel.create({
                roleName,
                roleLevel
            })
            return result
        } catch(error) {
            console.log("create role error:",error)
            return null
        }
    },
    deleteRoleById: async(id) => {
        try {
            let result = await roleModel.destroy({
                where: {
                    id
                }
            })
            return result
        } catch(error) {
            console.log("delete role by id error:",error)
            return false
        }
    }, 
    updateRole: async(id,roleName,roleLevel) => {
        try {
            let result = await roleModel.update({
                roleName,
                roleLevel
            },{
                where: {
                    id
                }
            })
            return result
        } catch(error) {
            console.log("update role error:",error)
            return null
        }
    }
}